package com.xinchen.aiowaste.vm

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.maple.model.data.AioUserInfo
import com.maple.model.entity.aio.FacilityListEntity
import com.maple.model.entity.aio.StorehouseListEntity
import com.maple.model.entity.aio.WarehousEntity
import com.maple.model.entity.aio.WarehouseListEntity
import com.maple.model.entity.aio.WasteListEntity
import com.maple.utils.store.AioDBHelper
import com.maple.utils.util.LogUtils
import com.xinchen.aiowaste.base.BaseViewModel
import com.xinchen.aiowaste.model.repository.CommonRepository
import java.util.WeakHashMap

class InboundWeightViewModel: BaseViewModel(){

    private val repository by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { CommonRepository() }

    val userInfo: AioUserInfo? by lazy {
        AioDBHelper.getUser()
    }

    val entId: String by lazy {
        userInfo?.entId?:""
    }

    val userId: String by lazy {
        userInfo?.userId?:""
    }

    val scalesNo: String by lazy {
        AioDBHelper.getScalesNo()
    }

    val facilityListData: MutableLiveData<List<FacilityListEntity.Data>> = MutableLiveData()

    fun getFacilityList() {
        launchOnlyResult(
            isShowDialog = false,
            block = {
            repository.facilityList(entId,"0")
        }, success = {
            val entity = GsonUtils.fromJson(GsonUtils.toJson(it), FacilityListEntity::class.java)
            if(!entity.data.isNullOrEmpty()) {
                facilityListData.postValue(entity.data)
            } else {
                defUI.onToast("产生设施无数据！")
            }
        }, error = {
            defUI.onToast("未获取到产生设施数据！")
        })
    }

    val warehouseListData: MutableLiveData<List<WarehouseListEntity.Data>> = MutableLiveData()

    fun getWarehouseList() {
        val params: WeakHashMap<String, Any> = WeakHashMap()
        params.put("entId", entId)
        launchOnlyResult(
            isShowDialog = false,
            block = {
            repository.warehouseList(params)
        }, success = {
            val entity = GsonUtils.fromJson(GsonUtils.toJson(it), WarehouseListEntity::class.java)
            if(!entity.data.isNullOrEmpty()) {
                warehouseListData.postValue(entity.data)
                getStorehouseList(entity.data.first().repositoryId,false)
            } else {
                defUI.onToast("贮存设施无数据！")
            }
        }, error = {
            defUI.onToast("未获取到贮存设施数据！")
        })
    }

    val storehouseListData: MutableLiveData<List<StorehouseListEntity.Data>> = MutableLiveData()

    fun getStorehouseList(repositoryId: String?,isShowDialog: Boolean? = true) {
        val params: WeakHashMap<String, Any> = WeakHashMap()
        params.put("wid", repositoryId)
        launchOnlyResult(
            isShowDialog = isShowDialog?:true,
            block = {
            repository.storehouseList(params)
        }, success = {
            val entity = GsonUtils.fromJson(GsonUtils.toJson(it), StorehouseListEntity::class.java)
            if(!entity.data.isNullOrEmpty()) {
                storehouseListData.postValue(entity.data)
            } else {
                defUI.onToast("贮存库位无数据！")
            }
        }, error = {
            defUI.onToast("未获取到贮存库位数据！")
        })
    }

    val warehousData: MutableLiveData<String> = MutableLiveData()

    fun inboundSubmit(params: WeakHashMap<String, Any>) {
        launchOnlyResult(
            block = {
                repository.inboundSubmit(params)
            }, success = {
                val entity = GsonUtils.fromJson(GsonUtils.toJson(it), WarehousEntity::class.java)
                entity.data?.let { data ->
                    if(!TextUtils.isEmpty(data.ewmwb)) {
                        warehousData.postValue(data.ewmwb)
                    } else {
                        defUI.onToast("入库失败,无数据！！")
                    }
                }?: let {
                    defUI.onToast("入库失败,无数据！")
                }
            })
    }


}